﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace LowLevelGraphics.Filter
{
    /// <summary>
    /// Trivial linear tone mapping operator.
    /// </summary>
    public sealed class Linear : Operator
    {
        public double Factor
        {

            get
            {
                return this.a;
            }
            set
            {
                this.a = Math.Min(Math.Max(value, 0.001), 1000);
            }
        }

        public double Shift
        {

            get
            {
                return this.b;
            }
            set
            {
                this.b = Math.Min(Math.Max(value, -1000), 1000);
            }
        }

        private double a = 1.0;
        private double b = 0;


        public Linear(double factor, double shift)
        {

            this.Factor = factor;
            this.Shift = shift;
        }

        public Linear()
        {
        }

        protected override void Run(float[][,] mapInput, int width, int height, ref float[][,] mapOutput)
        {

            for (int c = 0; c < 3; c++)
                for (int y = 0; y < height; y++)
                    for (int x = 0; x < width; x++)
                        mapOutput[c][x, y] = (float)Math.Min(Math.Max(mapInput[c][x, y] * this.a + this.b, 0), 1);
        }

        protected override void SetParam(string param, string value)
        {

            switch (param)
            {
                case "factor":
                    this.Factor = Double.Parse(value);
                    break;
                case "shift":
                    this.Shift = Double.Parse(value);
                    break;
            }
        }
    }
}
